/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dominio;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
 *
 * @author ronan
 */
@Entity
@Table(name = "aluno")
public class Aluno implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private int codigoAluno;
    @Column(length = 100)
    private String nome;
    @Column(length = 14)
    private String cpf;
    @Column(length = 20)
    private String senha;
    @ManyToMany(targetEntity = model.dominio.Turma.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "turma_aluno", joinColumns = {
        @JoinColumn(name = "condigoAluno")}, inverseJoinColumns = {
        @JoinColumn(name = "codigoTurma")})
    private List <Turma> turmas;

    public Aluno() {
    }

    public Aluno(String nome, String cpf) {
        this();
        this.nome = nome;
        this.cpf = cpf;
    }

    public Aluno(String dados) {
        this();
        String[] arrayDados = dados.split("%%%");
        this.codigoAluno = Integer.parseInt(arrayDados[0]);
        this.nome = arrayDados[1];
        this.cpf = arrayDados[2];
    }

    /**
     * @return the cpf
     */
    public String getCpf() {
        return cpf;
    }

    /**
     * @param cpf the cpf to set
     */
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    /**
     * @return the aluno
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param aluno the aluno to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    @Override
    public String toString() {
        return this.getCodigoAluno() + "%%%" + this.nome + "%%%" + this.cpf;
    }

    /**
     * @return the codigoAluno
     */
    public int getCodigoAluno() {
        return codigoAluno;
    }

    /**
     * @param codigoAluno the codigoAluno to set
     */
    public void setCodigoAluno(int codigoAluno) {
        this.codigoAluno = codigoAluno;
    }
    
    public int compareTo(Aluno a){
        return this.nome.compareTo(a.getNome());
    }

    /**
     * @return the turmas
     */
    public List <Turma> getTurmas() {
        return turmas;
    }

    /**
     * @param turmas the turmas to set
     */
    public void setTurmas(List <Turma> turmas) {
        this.turmas = turmas;
    }

    /**
     * @return the senha
     */
    public String getSenha() {
        return senha;
    }

    /**
     * @param senha the senha to set
     */
    public void setSenha(String senha) {
        this.senha = senha;
    }
    
}